local guwu = fk.CreateSkill {
  name = "lvshi__guwu",
}

Fk:loadTranslationTable{
  ["lvshi__guwu"] = "故舞",
  [":lvshi__guwu"] = "其他角色的回合结束时，你可依次重铸X张花色不同的手牌（X为中央区缺少的花色数），然后若中央区内的花色为4，你可令一名角色视为对其使用【决斗】。",
  ["#lvshi_guwu-recast"] = "故舞：你可重铸一张此次未重铸过花色的牌",
  ["#lvshi__guwu-choose"] = "故舞：你可令一名角色视为对%src使用【决斗】",

  ["$lvshi__guwu1"] = "弦鼓为谁歌，旋舞因何转。",
  ["$lvshi__guwu2"] = "未竟之舞，不可再得。",
}

local DIY = require "packages/diy_utility/diy_utility"
local ls = require "packages/lvshi/utility"

guwu:addEffect(fk.TurnEnd, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(guwu.name) then
      return target ~= player and ls.countSuits(DIY.getCenterCards(player.room)) < 4
    end
  end,
  on_cost = function(self, event, target, player, data)
    local cards = player.room:askToCards(player, {
      min_num = 1,
      max_num = 1,
      skill_name = guwu.name,
      prompt = "#lvshi_guwu-recast",
      cancelable = true,
    })
    if #cards > 0 then
      event:setCostData(self, {cards = cards})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = 3 - ls.countSuits(DIY.getCenterCards(player.room))
    room:recastCard(event:getCostData(self).cards, player)
    local suits = {"spade", "heart", "club", "diamond"}
    local suit = Fk:getCardById(event:getCostData(self).cards[1]):getSuitString()
    table.removeOne(suits, suit)
    while #suits > 0 and n > 0 do
      local cards = player.room:askToCards(player, {
        min_num = 1,
        max_num = 1,
        include_equip = false,
        skill_name = guwu.name,
        pattern = ".|.|" .. table.concat(suits, ","),
        prompt = "#lvshi_guwu-recast",
        cancelable = true,
      })
      if #cards == 0 then break end
      table.removeOne(suits, Fk:getCardById(cards[1]):getSuitString())
      room:recastCard(cards, player)
      n = n - 1
    end
    if ls.countSuits(DIY.getCenterCards(room)) == 4 and not target.dead then
      local card = Fk:cloneCard("duel")
      local to = room:askToChoosePlayers(player, {
        targets = table.filter(room:getOtherPlayers(target), function (p) return p:canUseTo(card, target) end),
        min_num = 1,
        max_num = 1,
        skill_name = guwu.name,
        cancelable = true,
        prompt = "#lvshi__guwu-choose:"..target.id,
      })
      if #to > 0 then
        room:useVirtualCard("duel", nil, to[1], target, "lvshi__guwu", true)
      end
    end
  end,
})

guwu:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

return guwu